﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Uniforax.Core.Common;
using Uniforax.Core.GameActions;
using Uniforax.Core.GameObjects;
using Uniforax.Core.Interfaces;

namespace Uniforax.Core.WorldFramework
{
    public class GameWorld : IExecutableHost
    {
        #region protected

        protected ActionContext CreateActionContext()
        {
            var actionContext = new ActionContext(this);
            return actionContext;
        }

        #endregion

        #region interface impls

        public virtual void Execute(BaseGameAction action)
        {
            var context = CreateActionContext();
            action.Execute(context);
        }

        #endregion

        #region methods

        public PlayerData GetPlayerData(int playerId)
        {
            return null;
        }

        public void PlaceObject(BaseGameObject gameObject, PosId posId)
        {
            var grid = GetGrid(posId);
        }

        public BaseGameObject GetPlacedObject(string objectId)
        {
            return null;
        }

        public BaseGameObject GetGameObject(PosId posId, string objectId)
        {
            return null;
        }

        public void RemoveGameObject(BaseGameObject gameObject)
        {

        }

        public Grid GetGrid(PosId posId)
        {
            return null;
        }

        public void RestoreTempGameObject(BaseGameObject gameObject, string objectId)
        {

        }

        public BaseGameObject GetRestoredTempGameObject(string objectId)
        {
            return null;
        }

        public void RemoveRestoredTempGameObject(BaseGameObject gameObject)
        {

        }

        public IEnumerable<BaseGameObject> GetRestoredGameObjects()
        {
            return null;
        }

        public IEnumerable<Grid> GetGrids()
        {
            return null;
        }

        public IEnumerable<BaseGameObject> Query()
        {
            foreach (var grid in GetGrids())
            {
                foreach (var gameObject in grid.GetGameObjects())
                {
                    yield return gameObject;
                }
            }

            foreach (var gameObject in GetRestoredGameObjects())
            {
                yield return gameObject;
            }
        }

        public IEnumerable<BaseGameObject> QueryDescendants()
        {
            foreach (var grid in GetGrids())
            {
                foreach (var gameObject in grid.GetGameObjects())
                {
                    yield return gameObject;
                    if (gameObject.HasChildren)
                    {
                        foreach (var child in gameObject.GetChildrenObjects())
                        {
                            yield return child;
                        }
                    }
                }
            }

            foreach (var gameObject in GetRestoredGameObjects())
            {
                yield return gameObject;
                if (gameObject.HasChildren)
                {
                    foreach (var child in gameObject.GetChildrenObjects())
                    {
                        yield return child;
                    }
                }
            }
        }


        #endregion

    }
}
